package edu.zcmu.seckill.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.zcmu.seckill.entity.Policy;
import edu.zcmu.seckill.entity.Product;
import edu.zcmu.seckill.entity.ProductPolicy;
import edu.zcmu.seckill.enumeration.GlobalEnum;
import edu.zcmu.seckill.exception.MyException;
import edu.zcmu.seckill.mapper.ProductMapper;
import edu.zcmu.seckill.response.ResultCode;
import edu.zcmu.seckill.service.ProductPolicyService;
import edu.zcmu.seckill.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;


/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 唐诗
 * @since 2022-02-22
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ProductPolicyService policyService;
    @Value("${product.default.img}")
    String defaultImg;
    //根据页码获取产品
    @Override
    public Map<String, Object> queryAllProduct(Long page) {
        HashMap<String, Object> hashMap = new HashMap();


        Page<Product> productPage = new Page<>();
        productPage.setCurrent(page);
        productPage.setSize(8L);
        productPage =baseMapper.selectPage(productPage,null);
        hashMap.put("count", productPage.getTotal());
        hashMap.put("data", productPage.getRecords());
        return hashMap;
    }

    //根据id查询产品信息
    @Override
    public Map<String,Object> getProductById(String id){
        HashMap<String, Object> map = new HashMap<>(2);
        Product product = baseMapper.selectOne(new LambdaQueryWrapper<Product>()
                .eq(!StringUtils.isEmpty(id), Product::getId, id));
        map.put("data",product);
        return map;
    }

    //上传商品缓存
    @Override
    public void uploadProductCache() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<Product> list = this.baseMapper.selectList(null);
        if (list != null && list.size() > 0) {
            list.forEach(item -> {
                if (StringUtils.isEmpty(item.getImg())){
                    item.setImg(defaultImg);
                }
                ProductPolicy policy = policyService.getProductPolicyById(item.getId());
                String productPolicy = encapsulationProductPolicy(policy);

                valueOperations.set(GlobalEnum.GET_PRODUCT_INFO_REDIS.getCode()+item.getId(),JSON.toJSONString(item));
                valueOperations.set(GlobalEnum.GET_PRODUCT_COUNT_REDIS.getCode() + item.getId(),item.getCount().toString());
                valueOperations.set(GlobalEnum.GET_PRODUCT_POLICY_REDIS.getCode()+ item.getId(),productPolicy);
            });
            valueOperations.set(GlobalEnum.GET_ALL_PRODUCT_REDIS.getCode(), JSON.toJSONString(list));
        }

    }

    @Override
    public void updateProductBySale(String id, int i) {

    }

    //封装产品初筛条件
    public String encapsulationProductPolicy(ProductPolicy policy){
        String policyCondition="";
        if (policy==null){
            return "111_18";
        }
        if (policy.getOverdue()==null||policy.getOverdue()==1){
            policyCondition=policyCondition+"1";
        }else{
            policyCondition=policyCondition+"0";
        }

        if (policy.getJob()==null||policy.getJob()==1){
            policyCondition=policyCondition+"1";
        }else{
            policyCondition=policyCondition+"0";
        }

        if (policy.getDishonesty()==null||policy.getDishonesty()==1){
            policyCondition=policyCondition+"1";
        }else{
            policyCondition=policyCondition+"0";
        }

        if (policy.getAgeSize()==null){
            policyCondition=policyCondition+"_18";
        }else {
            policyCondition=policyCondition+"_"+policy.getAgeSize().toString();
        }
        return policyCondition;

    }
    //设置产品默认图片
    public Product uploadImg(Product product){
        if(StringUtils.isEmpty(product)){
            product.setImg(defaultImg);
        }
        return product;
    }


    //根据页码获取八条数据
    public List getEightInfo(List list, Integer page) {
        if (list == null || list.size() <= 0) {
            return null;
        }
        ArrayList<Object> objects = new ArrayList(8);
        int index = (page - 1) * 8;
        int length = list.size();
        for (int i = 0; i < 8; i++) {
            if (index < length) {
                objects.add(list.get(index++));
            } else {
                break;
            }
        }
        return objects;
    }

}
